package com.czy.owner.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.entity.OnLineOrderServiceModel;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.widget.shopbutton.AnimShopButton;
import com.czy.owner.widget.shopbutton.IOnAddDelListener;

import java.util.List;

/**
 * Created by hedepu on 2017-08-29
 */

public class OrderProjectAdapter extends BaseRecyclerAdapter<SelectBespokeListModel, OrderProjectAdapter.OrderProjectHolder> {
    private Context ctx;
    private Toast toast;
    private onDataLine onDataLine;

    public OrderProjectAdapter(Context context, List<SelectBespokeListModel> data) {
        super(context, data);
        this.ctx = context;
    }

    public void setOnDataLine(OrderProjectAdapter.onDataLine onDataLine) {
        this.onDataLine = onDataLine;
    }

    public void addALL(List<SelectBespokeListModel> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(OrderProjectHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));

    }

    public List<SelectBespokeListModel> getList() {
        return data;
    }

    @Override
    public OrderProjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderProjectHolder(layoutInflater.inflate(R.layout.item_order_project, parent, false));
    }

    public class OrderProjectHolder extends BaseRecyclerAdapter.ViewHolder {
        private AnimShopButton btn_anmi_shop;
        private TextView tv_card_name, tv_money, tv_deposit;

        public OrderProjectHolder(View itemView) {
            super(itemView);
            btn_anmi_shop = (AnimShopButton) itemView.findViewById(R.id.btn_anmi_shop);
            tv_card_name = (TextView) itemView.findViewById(R.id.tv_card_name);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_deposit = (TextView) itemView.findViewById(R.id.tv_deposit);


        }

        public void bindData(final SelectBespokeListModel model) {
            btn_anmi_shop.setCount(model.getCount());
            btn_anmi_shop.setOnAddDelListener(new IOnAddDelListener() {
                @Override
                public void onAddSuccess(int count) {
                    model.setCount(count);
                    if (onDataLine != null)
                        onDataLine.onSunccess();
                }

                @Override
                public void onAddFailed(int count, FailType failType) {
                    if (failType == FailType.COUNT_MAX)
                        showToastShort("最多不能超出最大次数");
                }

                @Override
                public void onDelSuccess(int count) {
                    model.setCount(count);
                    if (count == 0) {
                        data.remove(model);
                        if (onDataLine != null)
                            onDataLine.onDelModel(model);
                        notifyDataSetChanged();
                    }
                    if (onDataLine != null)
                        onDataLine.onSunccess();

                }

                @Override
                public void onDelFaild(int count, FailType failType) {

                }
            });
            tv_card_name.setText(model.getServiceName());
            try {

                tv_money.setText("¥" + (Double.parseDouble(model.getStoreMemberPrice()) > 0 ? DecimalUtil.formatNum(model.getStoreMemberPrice())
                        : DecimalUtil.formatNum(model.getServicePrice())));

            }catch (Exception e){
                tv_money.setText("¥" +DecimalUtil.formatNum(model.getServicePrice()));
            }

            tv_deposit.setText("¥" + DecimalUtil.formatNum(model.getReservationPrice()));

//            btn_anmi_shop.setMinCount(1);
        }
    }

    public void showToastShort(String message) {
        if (toast == null) {
            toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
        } else {
            toast.setText(message);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        toast.show();
    }

    public interface onDataLine {
        void onSunccess();

        void onDelModel(SelectBespokeListModel model);
    }

}

